package  
{
	import net.flashpunk.World;
	import flash.media.Camera;
	import net.flashpunk.FP;
	import flash.utils.ByteArray;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	/**
	 * © 2012 TheJavaArcade.com
	 */	
	public class GameEngine extends World{
		public static var fracas:Fracas;
		public static var fruit:Vector.<Fruit> = new Vector.<Fruit>;
		public static var dialogue:Vector.<Dialogue> = new Vector.<Dialogue>;
		public static var levelXML:Vector.<XML> = new Vector.<XML>;
	//individual entities
		public static var kong:Kong;
		public static var kongDialogue:Dialogue;
		public static var mario:Mario;
		public static var marioDialogue:Dialogue;	
		public static var fly1:Fly1;
		public static var fly1Dialogue:Dialogue;
		public static var fly2:Fly2;
		public static var fly2Dialogue:Dialogue;
		public static var bee:Bee;
		public static var beeDialogue:Dialogue;
		public static var butterfly:Butterfly;
		public static var butterflyDialogue:Dialogue;
		public static var worm:Worm;
		public static var wormDialogue:Dialogue;
		public static var hornet:Hornet;
		public static var hornetDialogue:Dialogue;
		public static var stinkbug:Stinkbug;
		public static var stinkbugDialogue:Dialogue;
		public static var hive:Hive;
		public static var heart:Heart;
		public static var umbrella:Umbrella;
		public static var file:File;
		public static var cagekey:Cagekey;
		public static var background:Background;
		public static var scoreDialogue:Dialogue;
		public static var gameoverDialogue:Dialogue;
		public static var nameDialogue:Dialogue;
		public static var enterDialogue:Dialogue;
		
		public static var exitN:Exit;
		public static var exitS:Exit;
		public static var exitE:Exit;
		public static var exitW:Exit;
		
		public static var isPaused:Boolean;
		public static var level:int;
		public static var score:int;
		public static var timer:int;
		
		public static var area:Vector.<Level> = new Vector.<Level>;
		public static var vine:Vector.<Vines> = new Vector.<Vines>;
		public static var nocollide:Vector.<Nocollide> = new Vector.<Nocollide>;
		public static var strengthBar:Strength;
		
		public var fruitCount:int;
		public var bugCount:int;
		
		public function GameEngine() {	
		//create & add the entities
			loadXMLLevels();
			bugCount = 0;
			fruitCount = 0;
			for (var i:int = 0; i < 11; i++) {
				area[i] = new Level(i);
				vine[i] = new Vines(i);
				nocollide[i] = new Nocollide(i);
				growFruit(i);
			}
			//EXITS
			GameEngine.exitE = new Exit("East", this);
			GameEngine.exitW = new Exit("West", this);
			GameEngine.exitN = new Exit("North", this);
			GameEngine.exitS = new Exit("South", this);
			add(exitE);
			add(exitN);
			add(exitS);
			add(exitW);
			GameEngine.fracas = new Fracas();
			add(fracas);
			GameEngine.strengthBar = new Strength();
			add(strengthBar);
			GameEngine.hive = new Hive(4);
			add(hive);
			GameEngine.heart = new Heart(1);
			add(heart);
			GameEngine.file = new File(6);
			add(file);
			GameEngine.umbrella = new Umbrella(3);
			add(umbrella);
			GameEngine.cagekey = new Cagekey(8);
			add(cagekey);
			//KONG
			kong = new Kong(0, this);
			add(kong);
			GameEngine.kongDialogue = new Dialogue();
			add(kongDialogue);
			kongDialogue.visible = false;
			kongDialogue.theText.text = "HELP! Some Italian guy locked me up!!\nPlease find the key!";
			//MARIO
			mario = new Mario(2);
			add(mario);
			GameEngine.marioDialogue = new Dialogue();
			add(marioDialogue);
			marioDialogue.visible = false;
			marioDialogue.theText.text = "Woe is me!!\nI have lost my key!";
			//FLY1
			fly1 = new Fly1(0);
			add(fly1);
			GameEngine.fly1Dialogue = new Dialogue()
			add(fly1Dialogue);
			fly1Dialogue.visible = false;
			fly1Dialogue.theText.text = "press 'Z' to jump\nhold 'UP' to grab & climb vines";
			//FLY2
			fly2 = new Fly2(0);
			add(fly2);
			GameEngine.fly2Dialogue = new Dialogue()
			add(fly2Dialogue);
			fly2Dialogue.visible = false;
			fly2Dialogue.theText.text = "Fruit will make you stronger";
			//BEE
			bee = new Bee(1);
			add(bee);
			GameEngine.beeDialogue = new Dialogue()
			add(beeDialogue);
			beeDialogue.visible = false;
			//BUTTERFLY
			butterfly = new Butterfly(3);
			add(butterfly);
			GameEngine.butterflyDialogue = new Dialogue();
			add(butterflyDialogue);
			butterflyDialogue.visible = false;
			//WORM
			worm = new Worm(6);
			add(worm);
			GameEngine.wormDialogue = new Dialogue();
			add(wormDialogue);
			wormDialogue.visible = false;
			//HORNET
			hornet = new Hornet(2);
			add(hornet);
			GameEngine.hornetDialogue = new Dialogue();
			add(hornetDialogue);
			hornetDialogue.visible = false;
			hornetDialogue.theText.align = "left";
			hornetDialogue.theText.text = "    I will teach you a new technique.\nPress 'Z' while in the air to DOUBLE JUMP";
			//STINKBUG
			stinkbug = new Stinkbug(2);
			add(stinkbug);
			GameEngine.stinkbugDialogue = new Dialogue();
			add(stinkbugDialogue);
			stinkbugDialogue.visible = false;
			stinkbugDialogue.theText.align = "left";
			stinkbugDialogue.theText.text = "          It's a long way to the top...\nLook for BANANAs WEST of where you started.";
			//BACKGROUND
			GameEngine.background = new Background();
			add(background);
			//Gameover score & text
			GameEngine.scoreDialogue = new Dialogue();
			add(scoreDialogue);
			GameEngine.gameoverDialogue = new Dialogue();
			add(gameoverDialogue);
			GameEngine.nameDialogue = new Dialogue();
			add(nameDialogue);
			GameEngine.enterDialogue = new Dialogue();
			add(enterDialogue);
			//SCORE & COUNTDOWN
			score = 0;
			timer = 104400;
			isPaused = false;
		//load level
			loadLevel(920, 504, 0, -1);
//				hive.state = 1;
//				cagekey.state = 2;
//				umbrella.state = 2;
//				fracas.jump = 10;
//				fracas.strength = 38;
//				fracas.speed = 4;
//				fracas.regeneration = 5;
//				fracas.canDoubleJump = true;
//				butterfly.state = 1;
//				file.state = 1;
		}
//
		override public function update():void {
			if(!isPaused) super.update();
			if (Input.pressed(Key.P) && !fracas.gameOver) isPaused = !isPaused;
		}
//
	//LOAD LEVEL
		public function loadLevel(fX:int, fY:int, theLevel:int, oldLevel:int):void {
			level = theLevel;
			if (level == 0) background.graphic = background.bg_1;
			if (level == 1 || level == 2 || level == 3 || level == 4 || level == 10) background.graphic = background.bg_2;
			if (level == 5 || level == 6 || level == 7 || level == 8 || level == 9) background.graphic = background.bg_3;
			
			if (oldLevel != -1) {
				remove(area[oldLevel]);
				remove(vine[oldLevel]);
				remove(nocollide[oldLevel]);
			}
			add(area[theLevel]);
			add(vine[theLevel]);
			add(nocollide[theLevel]);
		//toggle entity/item visiblilty based on level	
			if (kong.level == level) kong.visible = true; else kong.visible = false;
			if (mario.level == level) mario.visible = true; else mario.visible = false;
			if (fly1.level == level) fly1.visible = true; else fly1.visible = false;
			if (fly2.level == level) fly2.visible = true; else fly2.visible = false;
			if (bee.level == level) bee.visible = true; else bee.visible = false;
			if (butterfly.level == level) butterfly.visible = true; else butterfly.visible = false;
			if (worm.level == level) worm.visible = true; else worm.visible = false;
			if (hornet.level == level) hornet.visible =  true; else hornet.visible = false;
			if (stinkbug.level == level) stinkbug.visible = true; else stinkbug.visible = false;
			if (heart.level == level && heart.state == 1) heart.visible = true; else heart.visible = false;
			if (file.level == level && file.state == 1) file.visible = true; else file.visible = false;
			if (umbrella.level == level && umbrella.state == 1) umbrella.visible = true; else umbrella.visible = false;
			if (cagekey.level == level && cagekey.state == 1) cagekey.visible = true; else cagekey.visible = false;
			
			if (umbrella.state == 2) umbrella.visible = true;
			if (level == 4 && hive.state == 0) hive.visible = true; else hive.visible = false;
			if (hive.state == 1) hive.visible = true;
			if (file.state == 2) file.visible = true;
			if (cagekey.state == 2 ) cagekey.visible = true;
			setFruit(theLevel);
			fracas.x = fX;
			fracas.y = fY;
			fracas.adjustCamera();
		}
	//SET FRUIT
		public function setFruit(theLevel:int):void {
			for (var i:int = 0; i < fruitCount; i++) {
				if (fruit[i].level == theLevel) {
					fruit[i].visible = true;
				}else {
					fruit[i].visible = false;
				}
			}
		}
	//GROW FRUIT
		public function growFruit(theLevel:int):void {
			var dataList:XMLList;
			var dataElement:XML;
			dataList = levelXML[theLevel].fruit.tile;
			for each(dataElement in dataList) {
				GameEngine.fruit[fruitCount] = new Fruit(theLevel);
				fruit[fruitCount].x = dataElement.@x * 15;
				fruit[fruitCount].y = dataElement.@y * 12;
				if (dataElement.@id == 0) fruit[fruitCount].type = "pear";
				if (dataElement.@id == 1) fruit[fruitCount].type = "apple";
				if (dataElement.@id == 2) fruit[fruitCount].type = "banana";
				if (dataElement.@id == 3) fruit[fruitCount].type = "egg";
				fruit[fruitCount].fruitSprite.play(fruit[fruitCount].type);
				add(fruit[fruitCount]);
				fruitCount++;
			}
		}
	//LOAD XML LEVELS
		public function loadXMLLevels():void {
			var theData:ByteArray;
			var dataString:String;
			for (var i:int = 0; i < 11; i++) {
				if (i == 0) theData = new Assets.LEVEL_0;
				if (i == 1) theData = new Assets.LEVEL_1;
				if (i == 2) theData = new Assets.LEVEL_2;
				if (i == 3) theData = new Assets.LEVEL_3;
				if (i == 4) theData = new Assets.LEVEL_4;
				if (i == 5) theData = new Assets.LEVEL_5;
				if (i == 6) theData = new Assets.LEVEL_6;
				if (i == 7) theData = new Assets.LEVEL_7;
				if (i == 8) theData = new Assets.LEVEL_8;
				if (i == 9) theData = new Assets.LEVEL_9;
				if (i == 10) theData = new Assets.LEVEL_10;
				dataString = theData.readUTFBytes(theData.length);
				levelXML[i] = new XML(dataString);
			}
		}
	}

}